//
//  TenderSecondController.swift
//  SlaughterClient
//
//  Created by 邱仙凯 on 2019/12/30.
//  Copyright © 2019 zwb. All rights reserved.
//

import UIKit

class MyChengjiaoVC: ZyaTenderBaseTableVC,UISearchBarDelegate {
    
    var page = 1
    
    
    var sort = "1"
    var searchTitle = ""
    var startDate = ""
    var endDate = ""
    var zcity = ""
    var dataArray = [ZyaZtbContentModel]()
    var z_zb_id = "" // 出价记录使用
    var minValue = ""
    var maxValue = ""
    private var searchBar:UISearchBar?
    
    @objc func doneClickss() {
        self.searchBar!.resignFirstResponder()
    }
    
    func setSearchBar () {
        searchBar = UISearchBar(frame:CGRect(x:15,y:3,width:KSGlobal.screenW - 30,height:40))
        let toolbar = UIToolbar()
        toolbar.barStyle = UIBarStyle.default
        toolbar.sizeToFit()
        let buttonflexible = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: nil, action: nil)
        let buttonDone = UIBarButtonItem(title: "完成", style: UIBarButtonItem.Style.done, target: self, action: #selector(doneClickss))
        buttonDone.tintColor = UIColor.black
        toolbar.setItems([buttonflexible, buttonDone], animated: true)
        
        searchBar!.inputAccessoryView = toolbar
        
    
        searchBar?.backgroundImage = UIImage()
       

        searchBar!.searchTextField.backgroundColor = .white
          searchBar!.searchTextField.layer.cornerRadius = 18
            searchBar!.searchTextField.layer.masksToBounds = true
        searchBar!.searchTextField.layer.borderColor = Theme.primaryColor().cgColor
       searchBar!.searchTextField.layer.borderWidth = 1
        
        
        searchBar!.text = searchTitle
        searchBar!.placeholder = "搜索成交信息"
        searchBar!.delegate = self
        let searchView = UIView(frame:CGRect(x:0,y:0,width:KSGlobal.screenW ,height:46))
        searchView.addSubview(searchBar!)
        self.tableView.tableHeaderView = searchView
        
    }
    func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
        searchBar.resignFirstResponder()
        searchTitle = searchBar.text.unwrap
        self.tableView.mj_header?.beginRefreshing()
    }
    
    
     var listType = TBandJPType.TB
     
     
     init(listType:TBandJPType) {
         super.init(nibName: nil, bundle: nil)
         self.listType = listType
     }
     required init?(coder aDecoder: NSCoder) {
         fatalError("init(coder:) has not been implemented")
     }
     
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setSearchBar()
        
        self.title = "我的成交"
        tableView.mj_header = KSRefreshHeader {
            self.page = 1
            self.network()
        }
        tableView.mj_footer = KSRefreshFooter {
            self.page += 1
            self.network()
        }
        tableView.mj_header?.beginRefreshing()
        tableView.tableFooterView = UIView()
        tableView.register(XSBaojiaCell.self, forCellReuseIdentifier: XSBaojiaCell.identifier)
        
        
        
        
        
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return self.dataArray.count
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        guard let cell = tableView.dequeueReusableCell(withIdentifier: XSBaojiaCell.identifier, for: indexPath) as? XSBaojiaCell else {
            fatalError("cell error")
        }
        cell.makeConstrainsts()
        let one = self.dataArray[indexPath.row]
        one.innerType = self.listType == .TB ? ZTBInnerType.zbAnnouncementKHW : .cjAnnouncementKHW
        one.listType = .TB
        cell.one = one
        cell.bottomStatusBg.statusBtn.setTitle("已成交", for: .normal)
        cell.bottomStatusBg.statusBtn.setBtnStyle( color: Theme.primaryColor())
        cell.chengjiaoImg.isHidden = true
 
        cell.btnDtradeType.isHidden = false
 
        return cell
    }
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        let one = self.dataArray[indexPath.row]
  
            let vc = ZyaWinInfoController(listType:self.listType,idkey: one.idKey.unwrap)
            self.navigationController?.pushViewController(vc, animated: true)
      
        
    }
    func network () {
        var api:API?
        if self.listType == .TB {
            api = API.tender(.ztbQueryMytenderWinList(z_city: "", page: "\(self.page)", size: "10",z_title: searchTitle))
        } else {
            api = API.tender(.queryAuctionList(z_city: "", page: "\(self.page)", size: "10",z_title: searchTitle))
        }
        
        
        KSNetwork.shared.doRequest(api!,showHud: false) { (data:BaseResult<ZyaZtbContentModel>) in
            if data.flag == TRUE {
                if let infos = data.infos {
                    if self.page == 1 {
                        self.dataArray.removeAll()
                    }
                    self.dataArray += infos
                    self.tableView.reloadData()
                    self.tableView.mj_header?.endRefreshing()
                    if infos.count < 10 {
                        self.tableView.mj_footer?.endRefreshingWithNoMoreData()
                    } else {
                        self.tableView.mj_footer?.endRefreshing()
                    }
                }
                
                
            } else {
                
            }
            
        }
    }
    
    
}

